package com.example.reserveseat.Utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class TimeUtil {

    /**
     * 获取时间区间列表
     *
     * @param start 开始时间
     * @param end   结束时间
     * @return 区间列表
     */
    public static List<String> getHourlyIntervals(String start, String end) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startDateTime = LocalDateTime.parse(start, formatter);
        LocalDateTime EndDateTime = LocalDateTime.parse(end, formatter);
        List<String> hourlyIntervals = new ArrayList<>();
        if (startDateTime.isAfter(EndDateTime) || startDateTime.equals(EndDateTime)) {
            return hourlyIntervals; // 如果开始时间在结束时间之后或相同，返回空列表
        }

        LocalDateTime current = startDateTime;

        while (!current.isAfter(EndDateTime)) {
            hourlyIntervals.add(current.format(formatter)); // 添加当前小时的开始时间
            current = current.plusHours(1); // 移动到下一个小时
        }


        return hourlyIntervals;
    }


    public static List<String> test(String start, String end) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startDateTime = LocalDateTime.parse(start, formatter);
        LocalDateTime endDateTime = LocalDateTime.parse(end, formatter); // 更改变量名为endDateTime
        List<String> hourlyIntervals = new ArrayList<>();

        // 如果开始时间在结束时间之后或相同，返回空列表
        if (startDateTime.isAfter(endDateTime) || startDateTime.equals(endDateTime)) {
            return hourlyIntervals;
        }

        // 循环直到当前时间超过结束时间
        LocalDateTime current = startDateTime;
        while (current.isBefore(endDateTime)) {
            // 直接在循环中格式化当前时间，并添加到列表中
            hourlyIntervals.add(current.format(formatter));
            // 移动到下一个小时的开始
            current = current.plusHours(1).withMinute(0).withSecond(0).withNano(0);
        }

        return hourlyIntervals;
    }

    public static String TimeDelayOneHour(String start){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startDateTime = LocalDateTime.parse(start, formatter);
        startDateTime = startDateTime.plusHours(1); // 移动到下一个小时
        return startDateTime.format(formatter);
    }
}
